package group2assignone.ucm.manager;

import group2assignone.dao.ClientComms;
import group2assignone.func.Authentication;
import group2assignone.model.Manager;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.MainView;
import group2assignone.view.UITool;
import group2assignone.view.manager.ManagerView;

/**
 * Use Case: Log in Manager
 * Pre-Condition: Manager inputs login details in the MainView UI
 * and presses the "login" button (with 'manager' selected)
 * Post-Condition: UI is updated
 * 1. System gets login details from view
 * 2. System authenticates login details
 * 3. System logs Manager in
 * 4. system updates UI
 * 2a.  - Customer provides invalid login details
 *      - System displays error message and prompts user to re-enter or cancel
 * 3a.  - System cannot log manager in (connection error, already logged in)
 *      - System displays error message
 * @author group2
 */
public class UCManagerLogin implements UCController {

    private Manager man;
    private MainView view;
    private Authentication auth;
    private boolean ok,  ok2;
    private ClientComms client;

    /**
     * sets the view
     * @param view
     */
    public UCManagerLogin(MainView view) {
        this.view = view;
        client = UCStartup.getInstance().getClientComms();
    }
    
    /**
     *Use Case Controller - logs manager in
     */
    @Override
    public void run() {
        //1. System gets login details from view
        man = new Manager();
        view.upload(man);
        //2. System authenticates login details
        auth = new Authentication();
        ok = auth.authManager(man);

        if (ok) {
            System.out.println(man);
            //3. System logs Manager in
            ok2 = client.login(man);
            if (ok2) {
                //4. system updates UI
                view.dispose(); //Remove current view
                ManagerView manView = ManagerView.getInstance();
                manView.setManager(man);
                manView.go();
                
            } else {    //3a.  - System cannot log manager in
                //(connection error, already logged in)
                //System displays error message
                UITool.displayError(client.getErrorMsg(), view);
            }
        } else {    //2a.  - Manager provides invalid login details
            //System displays error message
            UITool.displayError(auth.getErrorMsg(), view);
        }
    }
}
